﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Aspfm.Main
{
    [Serializable()]
    public class WebDataTable
    {
        public string TableName = "";

        public List<Hashtable> Rows = new List<Hashtable>();
        public static WebDataTable DataTableToWeb(DataTable dt)
        {
            if (dt != null)
            {
                WebDataTable web = new WebDataTable();
                web.TableName = dt.TableName;
                foreach (DataRow dr in dt.Rows)
                {
                    web.Rows.Add((new AspfmModel(dr)).Values);
                }
                return web;
            }
            return null;
        }
        public static List<WebDataTable> DataSetToWeb(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0)
            {
                List<WebDataTable> weblist = new List<WebDataTable>();
                foreach (DataTable dt in ds.Tables)
                {
                    weblist.Add(DataTableToWeb(dt));
                }
                return weblist;
            }
            return null;
        }
    }
    [Serializable()]
    public class WebResult
    {
        public WebResult(){}
        public WebResult(string _ErrMsg)
        {
            Success = false;
            ErrMsg = _ErrMsg;
        }
        public decimal AllCount = 0;
        public List<WebDataTable> WebDataSet = new List<WebDataTable>();
        public AspfmModel Result = new AspfmModel();
        public bool Success = true;
        public string ErrMsg = "";
    }
    [Serializable()]
    public class WebResultList
    {
        public WebResultList() { }
        public WebResultList(List<WebResult> _BWebResult)
        {
            BWebResult = _BWebResult;

            if (BWebResult != null && BWebResult.Count > 0)
            {
                foreach (WebResult strbw in BWebResult)
                {
                    ErrMsg += strbw.ErrMsg;
                    if (strbw.Success == false)
                    {
                        Success = false;
                    }
                }
            }
        }
        public List<WebResult> BWebResult = new List<WebResult>();
        public string ErrMsg = "";
        public bool Success = true;

        public static WebResultList DeserializeObject(string json)
        {
            if (!string.IsNullOrEmpty(json))
            {
                if (json.StartsWith("{\"BWebResult\":"))
                {
                    return DeserializeDynamic<WebResultList>(json);
                }
            }
            return null;
        }
        public static T DeserializeDynamic<T>(string json)
        {
            if (!string.IsNullOrEmpty(json))
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            }
            return default(T);
        }
        public static string SerializeObject(object obj)
        {
            if (obj != null)
            {
                //为了参数统一，所有接口返回都用WebResultList。
                if (obj is WebResult)
                {
                    List<WebResult> weblist = new List<WebResult>();
                    weblist.Add((WebResult)obj);
                    return Newtonsoft.Json.JsonConvert.SerializeObject(new WebResultList(weblist));
                }
                else if (obj is string || obj is decimal)
                {
                    return obj.ToString();
                }
                else if (obj is DataTable)
                {
                    List<WebResult> weblist = new List<WebResult>();
                    WebResult webrs = new WebResult();
                    webrs.WebDataSet.Add(WebDataTable.DataTableToWeb((DataTable)obj));
                    weblist.Add(webrs);
                    return Newtonsoft.Json.JsonConvert.SerializeObject(new WebResultList(weblist));
                }
                else if (obj is DataSet)
                {
                    List<WebResult> weblist = new List<WebResult>();
                    WebResult webrs = new WebResult();
                    webrs.WebDataSet = WebDataTable.DataSetToWeb((DataSet)obj);
                    weblist.Add(webrs);
                    return Newtonsoft.Json.JsonConvert.SerializeObject(new WebResultList(weblist));
                }
                else if (obj is List<WebResult>)
                {
                    WebResultList webresultlist = new WebResultList((List<WebResult>)obj);
                    return Newtonsoft.Json.JsonConvert.SerializeObject(webresultlist);
                }
                return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            }
            return "";
        }
    }
}
